﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace StudentInfoSystem.Core.Utils;

public static class SecurityHelper
{
    // 使用更安全的加密密钥
    private static readonly byte[] Key = Encoding.UTF8.GetBytes("YourSecureKeyHere1234567890!@#$%^"); // 32字节密钥用于AES

    public static string SimpleEncrypt(string input)
    {
        if (string.IsNullOrEmpty(input)) return input;

        try
        {
            using (Aes aes = Aes.Create())
            {
                aes.Key = Key;
                aes.IV = new byte[16]; // 初始向量设为全零，实际应用中应该使用随机IV

                ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter sw = new StreamWriter(cs))
                        {
                            sw.Write(input);
                        }
                    }
                    return Convert.ToBase64String(ms.ToArray());
                }
            }
        }
        catch
        {
            // 降级到简单XOR加密
            byte[] inputBytes = Encoding.UTF8.GetBytes(input);
            for (int i = 0; i < inputBytes.Length; i++)
            {
                inputBytes[i] = (byte)(inputBytes[i] ^ 0x5A);
            }
            return Convert.ToBase64String(inputBytes);
        }
    }

    // 解密方法
    public static string SimpleDecrypt(string input)
    {
        if (string.IsNullOrEmpty(input)) return input;

        try
        {
            // 先尝试AES解密
            byte[] buffer = Convert.FromBase64String(input);

            // 检查数据长度是否有效
            if (buffer.Length == 0) return string.Empty;

            using (Aes aes = Aes.Create())
            {
                aes.Key = Key;
                aes.IV = new byte[16];

                ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

                using (MemoryStream ms = new MemoryStream(buffer))
                {
                    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader sr = new StreamReader(cs))
                        {
                            return sr.ReadToEnd();
                        }
                    }
                }
            }
        }
        catch (FormatException)
        {
            // 不是有效的Base64字符串
            return string.Empty;
        }
        catch (CryptographicException)
        {
            // AES解密失败，尝试降级到XOR
            try
            {
                byte[] inputBytes = Convert.FromBase64String(input);
                for (int i = 0; i < inputBytes.Length; i++)
                {
                    inputBytes[i] = (byte)(inputBytes[i] ^ 0x5A);
                }
                return Encoding.UTF8.GetString(inputBytes);
            }
            catch
            {
                return string.Empty;
            }
        }
        catch
        {
            return string.Empty;
        }
    }
}